/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ejbgame;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;

/**
 *
 * @author automne
 */
public class TimeThread extends Thread {

    private JLabel lbl;
    private int time;

    public TimeThread(JLabel lbl, int time) {
        this.lbl = lbl;
        this.time = time;
    }

    public void setAttributes(JLabel lbl,int time) {
        this.lbl = lbl;
        this.time = time;
    }

    public void run() {
        while (time >= 0) {
            String timeFormat = formatTime(time);
            this.lbl.setText(timeFormat);
            time--;
            try {
                this.sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(TimeThread.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    private String formatTime(int time) {
        int min = (int)time/60;
        String minutes = "00";
        if (min<10) {
            minutes = "0" + String.valueOf(min);
        } else {
            minutes = String.valueOf(min);
        }
        int sec = time - 60*min;
        String seconds = "00";
        if (sec<10) {
            seconds = "0" + String.valueOf(sec);
        } else {
            seconds = String.valueOf(sec);
        }
        return (minutes + ":" + seconds);
    }
}
